package com.joysuch.wwyt.bp.service.impl;

import com.joysuch.wwyt.bp.bean.investigate.BpInvestigateTypeBean;
import com.joysuch.wwyt.bp.entity.investigate.BpInvestigateType;
import com.joysuch.wwyt.bp.repository.BpInvestigatePlanRepository;
import com.joysuch.wwyt.bp.repository.BpInvestigateStoreDetailRepository;
import com.joysuch.wwyt.bp.repository.BpInvestigateStoreRepository;
import com.joysuch.wwyt.bp.repository.BpInvestigateTypeRepository;
import com.joysuch.wwyt.bp.service.BpInvestigateTypeService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/3/25 16:22
 */
@Service
@Transactional
public class BpInvestigateTypeServiceImpl implements BpInvestigateTypeService {

    @Autowired
    private BpInvestigateStoreDetailRepository detailRepository;
    @Autowired
    private BpInvestigateStoreRepository storeRepository;
    @Autowired
    private BpInvestigatePlanRepository planRepository;
    @Autowired
    private BpInvestigateTypeRepository typeRepository;


    @Override
    public ResultBean save(BpInvestigateTypeBean bean) {
        Long id = bean.getId();
        String checkType = bean.getCheckType();
        int i = typeRepository.countByCheckType(checkType);
        BpInvestigateType bpInvestigateType;
        if (id == null) {
            if (i > 0) {
                return ResultBean.fail(101, "此排查类型已存在");
            }
            bpInvestigateType = new BpInvestigateType();
        } else {
            bpInvestigateType = typeRepository.findById(id).orElseThrow(() -> new RuntimeException("数据不存在"));
            if (!checkType.equals(bpInvestigateType.getCheckType())) {
                if (i > 0) {
                    return ResultBean.fail(101, "此排查类型已存在");
                }
            }
        }
        BeanUtils.copyProperties(bean, bpInvestigateType);
        typeRepository.save(bpInvestigateType);
        return ResultBean.defaultSuccessResult();
    }

    @Override
    public ResultBean delete(Long id) {
        typeRepository.deleteById(id);
        return ResultBean.defaultSuccessResult();
    }

    @Override
    public ResultBean page(Pageable pageable) {
        if (pageable.getPageSize() == 20) {
            List<BpInvestigateType> all = typeRepository.findAll();
            for (BpInvestigateType bpInvestigateType : all) {
                int i = planRepository.countByTypeId(bpInvestigateType.getId());
                bpInvestigateType.setUseSum(i);
            }
            return ResultBean.success(all);
        } else {
            pageable = PageDataUtils.addOrderByDesc(pageable, "createTime");
            Page<BpInvestigateType> page = typeRepository.findAll(pageable);
            for (BpInvestigateType bpInvestigateType : page) {
                int i = planRepository.countByTypeId(bpInvestigateType.getId());
                bpInvestigateType.setUseSum(i);
            }
            return ResultBean.pageData(page.getContent(), page.getTotalElements());
        }
    }

    @Override
    public BpInvestigateType getById(Long id) {
        return typeRepository.findById(id).orElse(new BpInvestigateType());
    }

    @Override
    public BpInvestigateType findFirstByCheckType(String checkType) {
        return typeRepository.findFirstByCheckType(checkType);
    }
}
